package com.example.ktsb.controller

import com.example.ktsb.entity.User
import com.example.ktsb.service.UserService
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("user")
class UserController(private val userService: UserService) {

    @PostMapping("add")
    fun addUser(@RequestBody user: User): User {
        return userService.addUser(user)
    }

    @PostMapping("update")
    fun updateUser(@RequestBody user: User): User {
        return userService.updateUser(user)
    }

    @PostMapping("del")
    fun delUser(userId: Long) {
        userService.deleteUser(userId)
    }

    @PostMapping("get")
    fun getUser(@RequestBody user: User): List<User> {
        return userService.getUserByName(user)
    }

    @PostMapping("getUserList")
    fun getUserList(@RequestBody user: User): List<User> {
        return userService.findUserList(user)
    }

    @PostMapping("getUserListByStatus")
    fun getUserListByStatus(status: Int, pageNum: Int, pageSize: Int) : Page<User> {
        val typedSort = Sort.sort(User::class.java)
        //Params:
        // page – zero-based page index, must not be negative.
        // size – the size of the page to be returned, must be greater than 0.
        // sort – must not be null, use Sort.unsorted() instead.
        val page = PageRequest.of(pageNum - 1, pageSize, typedSort.by(User::userName).ascending())
        return userService.findUserListByStatus(status, page)
    }
}